<template>
	<view>
		<view class="btns">
			<u-button size="small" @click="cancel">取消</u-button>
			<u-button size="small" type="success" @click="post">发布</u-button>
		</view>
		<view class="content">
			<u-textarea v-model="content" placeholder="一些分享..."></u-textarea>
		</view>
		<view class="picBox">
			<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="5"
				:previewFullImage="true"></u-upload>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
				fileList: []
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			// 新增图片
			afterRead(event) {
				var that = this
				const file = event.file[0]
				uni.uploadFile({
					url: 'http://192.168.31.240:8080/oss/fileoss',
					filePath: file.url,
					name: 'file',
					success: (uploadFileRes) => {
						const res = JSON.parse(uploadFileRes.data)
						that.fileList.push({
							url: res.data.url
						})
						console.log(that.fileList)
					},
				});
			},
			// 取消按钮
			cancel(){
				uni.navigateBack()
			},
			// 确认发布按钮
			async post(){
				var that = this;
				const{data:res} =await this.$myRequest({
					url:'/PyqBefore/addPyq',
					method:'post',
					data:{
						userId:uni.getStorageSync('userId'),
						pyqContent:this.content,
						pyqPicture:this.fileList[0].url,
					}
				})
				if(res.code === 20000){
					uni.showToast({
						title:'发布成功！',
						icon:'success'
					})
					setTimeout(()=>{
						uni.navigateBack()
					},1500)
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.btns {
		padding: 20rpx 0;
		width: 100%;
		height: 60rpx;
		display: flex;
		justify-content: space-between;

		/deep/.u-button {
			width: 20rpx;
			position: absolute;

			&:first-child {
				left: 20rpx;
			}

			&:last-child {
				right: 20rpx;
			}
		}
	}

	.content {
		margin: 0 20rpx;

		/deep/.u-textarea {
			border: none;
		}
	}
	.picBox{
		margin: 20rpx;
	}
</style>
